<template>
  <div id="admin">
    <h1 class="adminh1">用户注册</h1>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      class="demo-ruleForm"
    >
      <el-form-item label="" prop="name"
        ><el-input
          type="text"
          autocomplete="off"
          v-model="ruleForm.name"
          prefix-icon="el-icon-user-solid"
          placeholder="请输入用户名"
        ></el-input
      ></el-form-item>
      <el-form-item label="" prop="pass"
        ><el-input
          type="password"
          autocomplete="off"
          v-model="ruleForm.pass"
          prefix-icon="el-icon-lock"
          placeholder="请输入密码"
        ></el-input
      ></el-form-item>
      <el-form-item label="" prop="checkPass"
        ><el-input
          type="password"
          autocomplete="off"
          v-model="ruleForm.checkPass"
          prefix-icon="el-icon-lock"
          placeholder="请输入密码"
        ></el-input
      ></el-form-item>
      <el-form-item class="btns">
        <el-button type="primary" @click="submitForm('ruleForm')"
          >注册</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
 
<script>
import user from "@/api/user";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };

    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    return {
      activeName: "second",
      ruleForm: {
        name: "",
        pass: "",
        checkPass: "",
      },
      rules: {
        name: [{ required: true, message: "请输入您的名称", trigger: "blur" }],
        pass: [
          { required: true, validator: validatePass, trigger: "blur" },
          {
            min: 6,
            max: 12,
            message: "长度在 6 到 12 个字符",
            trigger: "blur",
          },
        ],
        checkPass: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
      },
    };
  },

  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          user
            .register(this.ruleForm.name, this.ruleForm.checkPass)
            .then(() => {

            });
          // this.activeName: 'first',
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style scoped>
.btns {
  display: flex;
  justify-content: flex-end;
}
.adminh1 {
  margin: 20px 0;
  text-shadow: 10px 13px 3px rgb(207, 207, 207);
  text-align: center;
}
#admin {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -200px;
  margin-left: -250px;
  width: 500px;
  height: 400px;
  background: #fff;
  border-radius: 10%;
  box-shadow: 8px 10px 10px rgb(177, 223, 242);
  padding: 20px;
}
</style>
